package jschtest;

import com.jcraft.jsch.*;
import io.SshBean;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.InputStream;

public class JschTool {
    private static final Logger LOG = Logger.getLogger(JschTool.class.getName());

    //执行单挑命令
    public static String execCommand(SshBean sshBean, String command){
        StringBuilder result = new StringBuilder();
        InputStream in = null;
        try {
            Session session = JschUtil.getSession(sshBean);
            session.connect();
            Channel channel=session.openChannel("exec");
            ((ChannelExec)channel).setCommand(command);

            ((ChannelExec)channel).setInputStream(null);
            ((ChannelExec)channel).setErrStream(System.err);

             in = ((ChannelExec)channel).getInputStream();

            ((ChannelExec)channel).connect();

            byte[] tmp = new byte[1024];
            while (true) {
                while (in.available() > 0) {
                    int i = in.read(tmp, 0, 1024);
                    if (i < 0) break;
                    result.append(new String(tmp, 0, i));
                }
                if (channel.isClosed()) {
                    if (in.available() > 0) {
                        continue;
                    }
                    System.out.println("exit-status: " + channel.getExitStatus());
                    break;
                }
                Thread.sleep(1000);
            }
            channel.disconnect();
            session.disconnect();
        } catch (JSchException e){
            System.out.println("establish ssh connect fail" + e);
        } catch (IOException e){
            System.out.println("get result data fail" + e);
        } catch (InterruptedException e){
            System.out.println("sleep occurs error" + e);
        }finally {
            if (in != null){
                try {
                    in.close();
                } catch (IOException e){
                    System.out.println("JschTool close inputstream error" + e);
                }
            }
        }
        return result.toString();
    }

    //上传文件
    public static boolean uploadFile(SshBean sshBean, String srcFile, String destDirectory){
        try {
            JschUtil jschUtil = new JschUtil();
            ChannelSftp chSftp =(ChannelSftp) jschUtil.getSftpChannel(sshBean, 60000);
//            File file = new File(srcFile);
            chSftp.put(srcFile, destDirectory, ChannelSftp.OVERWRITE); // 代码段2
//            long fileSize = file.length();
            chSftp.quit();
            jschUtil.closeSftpChannel();
        } catch (JSchException e){
            System.out.println("upload file establish connection exception:" + e);
            return false;
        }catch (SftpException e){
            System.out.println("upload file exception:" + e);
            return false;
        } catch (Exception e){
            System.out.println("upload file close disconnect exception:" + e);
            return false;
        }
        return true;
    }

    //下载文件
    public static boolean downloadFile(SshBean sshBean, String srcFile, String destDirectory){
        try {
            JschUtil jschUtil = new JschUtil();
            ChannelSftp chSftp = jschUtil.getSftpChannel(sshBean, 60000);
//            File file = new File(srcFile);
            chSftp.get(srcFile, destDirectory); // 代码段2
//            long fileSize = file.length();
            chSftp.quit();
            jschUtil.closeSftpChannel();
        } catch (JSchException e){
            System.out.println("download file establish connection exception" + e);
            return false;
        }catch (SftpException e){
            System.out.println("download file exception" + e);
            return false;
        } catch (Exception e){
            System.out.println("download file close disconnect exception" + e);
            return false;
        }
        return true;
    }
    //删除文件
    public static boolean deleteFile(SshBean sshBean, String filePath){
        try {
            JschUtil jschUtil = new JschUtil();
            ChannelSftp chSftp = jschUtil.getSftpChannel(sshBean, 60000);
            chSftp.rm(filePath);
            chSftp.quit();
            jschUtil.closeSftpChannel();
        } catch (JSchException e){
            System.out.println("delete file establish connection exception" + e);
            return false;
        }catch (SftpException e){
            System.out.println("delete file exception" + e);
            return false;
        } catch (Exception e){
            System.out.println("delete file close disconnect exception" + e);
            return false;
        }
        return true;
    }
}
